<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>简书函数封装 | 孙志豪的博客</title><meta name="author" content="孙志豪"><meta name="copyright" content="孙志豪"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="简书函数封装1、输入一个值，返回其数据类型**123function type(para) &amp;#123;    return Object.prototype.toString.call(para)&amp;#125;  2、数组去重1234567891011121314151617181920212223function unique1(arr) &amp;#123;    return [...new Se">
<meta property="og:type" content="article">
<meta property="og:title" content="简书函数封装">
<meta property="og:url" content="https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E7%AE%80%E4%B9%A6%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/index.html">
<meta property="og:site_name" content="孙志豪的博客">
<meta property="og:description" content="简书函数封装1、输入一个值，返回其数据类型**123function type(para) &amp;#123;    return Object.prototype.toString.call(para)&amp;#125;  2、数组去重1234567891011121314151617181920212223function unique1(arr) &amp;#123;    return [...new Se">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://szhihao.gitee.io/img/%E5%A4%A9%E7%A9%BA.jpeg">
<meta property="article:published_time" content="2022-10-11T12:01:36.000Z">
<meta property="article:modified_time" content="2022-10-11T09:13:15.729Z">
<meta property="article:author" content="孙志豪">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://szhihao.gitee.io/img/%E5%A4%A9%E7%A9%BA.jpeg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E7%AE%80%E4%B9%A6%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '简书函数封装',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-11 17:13:15'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/%E5%A4%B4%E5%83%8F.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">44</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">15</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/%E5%A4%A9%E7%A9%BA.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">孙志豪的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">简书函数封装</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-10-11T09:13:15.729Z" title="Updated 2022-10-11 17:13:15">2022-10-11</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/">核心js（手写篇）</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="简书函数封装"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><hr>
<h1 id="简书函数封装"><a href="#简书函数封装" class="headerlink" title="简书函数封装"></a>简书函数封装</h1><h3 id="1、输入一个值，返回其数据类型"><a href="#1、输入一个值，返回其数据类型" class="headerlink" title="1、输入一个值，返回其数据类型**"></a>1、输入一个值，返回其数据类型**</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">type</span>(<span class="params">para</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(para)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2、数组去重"><a href="#2、数组去重" class="headerlink" title="2、数组去重"></a>2、数组去重</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">unique1</span>(<span class="params">arr</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> [...<span class="keyword">new</span> <span class="title class_">Set</span>(arr)]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">unique2</span>(<span class="params">arr</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">return</span> arr.<span class="title function_">filter</span>(<span class="function"><span class="params">ele</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!obj[ele]) &#123;</span><br><span class="line">            obj[ele] = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">unique3</span>(<span class="params">arr</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> result = [];</span><br><span class="line">    arr.<span class="title function_">forEach</span>(<span class="function"><span class="params">ele</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (result.<span class="title function_">indexOf</span>(ele) == -<span class="number">1</span>) &#123;</span><br><span class="line">            result.<span class="title function_">push</span>(ele)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3、字符串去重"><a href="#3、字符串去重" class="headerlink" title="3、字符串去重"></a>3、字符串去重</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">String.prototype.unique = function () &#123;</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;&#125;,</span><br><span class="line">        str = <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!obj[<span class="keyword">this</span>[i]]) &#123;</span><br><span class="line">            str += <span class="keyword">this</span>[i];</span><br><span class="line">            obj[<span class="keyword">this</span>[i]] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> str;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">###### <span class="comment">//去除连续的字符串 </span></span><br><span class="line">function uniq(str) &#123;</span><br><span class="line">    <span class="keyword">return</span> str.replace(/(\w)\<span class="number">1</span>+/g, <span class="string">&#x27;$1&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4、深拷贝-浅拷贝"><a href="#4、深拷贝-浅拷贝" class="headerlink" title="4、深拷贝 浅拷贝"></a>4、深拷贝 浅拷贝</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//深克隆（深克隆不考虑函数）</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">deepClone</span>(<span class="params">obj, result</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> result = result || &#123;&#125;;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> obj) &#123;</span><br><span class="line">        <span class="keyword">if</span> (obj.<span class="title function_">hasOwnProperty</span>(prop)) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">typeof</span> obj[prop] == <span class="string">&#x27;object&#x27;</span> &amp;&amp; obj[prop] !== <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="comment">// 引用值(obj/array)且不为null</span></span><br><span class="line">                <span class="keyword">if</span> (<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(obj[prop]) == <span class="string">&#x27;[object Object]&#x27;</span>) &#123;</span><br><span class="line">                    <span class="comment">// 对象</span></span><br><span class="line">                    result[prop] = &#123;&#125;;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">// 数组</span></span><br><span class="line">                    result[prop] = [];</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="title function_">deepClone</span>(obj[prop], result[prop])</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 原始值或func</span></span><br><span class="line">        result[prop] = obj[prop]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 深浅克隆是针对引用值</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">deepClone</span>(<span class="params">target</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> (target) !== <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> target;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> result;</span><br><span class="line">    <span class="keyword">if</span> (<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(target) == <span class="string">&#x27;[object Array]&#x27;</span>) &#123;</span><br><span class="line">        <span class="comment">// 数组</span></span><br><span class="line">        result = []</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 对象</span></span><br><span class="line">        result = &#123;&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> target) &#123;</span><br><span class="line">        <span class="keyword">if</span> (target.<span class="title function_">hasOwnProperty</span>(prop)) &#123;</span><br><span class="line">            result[prop] = <span class="title function_">deepClone</span>(target[prop])</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 无法复制函数</span></span><br><span class="line"><span class="keyword">var</span> o1 = jsON.<span class="title function_">parse</span>(jsON.<span class="title function_">stringify</span>(obj1));</span><br></pre></td></tr></table></figure>

<h3 id="5、reverse底层原理和扩展"><a href="#5、reverse底层原理和扩展" class="headerlink" title="5、reverse底层原理和扩展"></a>5、reverse底层原理和扩展</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 改变原数组</span></span><br><span class="line">Array.prototype.myReverse = function () &#123;</span><br><span class="line">    <span class="keyword">var</span> len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> temp = <span class="keyword">this</span>[i];</span><br><span class="line">        <span class="keyword">this</span>[i] = <span class="keyword">this</span>[len - <span class="number">1</span> - i];</span><br><span class="line">        <span class="keyword">this</span>[len - <span class="number">1</span> - i] = temp;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6、圣杯模式的继承"><a href="#6、圣杯模式的继承" class="headerlink" title="6、圣杯模式的继承"></a>6、圣杯模式的继承</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">inherit</span>(<span class="params">Target, Origin</span>) &#123;</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">F</span>(<span class="params"></span>) &#123;&#125;;</span><br><span class="line">    F.<span class="property"><span class="keyword">prototype</span></span> = <span class="title class_">Origin</span>.<span class="property"><span class="keyword">prototype</span></span>;</span><br><span class="line">    <span class="title class_">Target</span>.<span class="property"><span class="keyword">prototype</span></span> = <span class="keyword">new</span> <span class="title function_">F</span>();</span><br><span class="line">    <span class="title class_">Target</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">constructor</span> = <span class="title class_">Target</span>;</span><br><span class="line">    <span class="comment">// 最终的原型指向</span></span><br><span class="line">    <span class="title class_">Target</span>.<span class="property">prop</span>.<span class="property">uber</span> = <span class="title class_">Origin</span>.<span class="property"><span class="keyword">prototype</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7、找出字符串中第一次只出现一次的字母"><a href="#7、找出字符串中第一次只出现一次的字母" class="headerlink" title="7、找出字符串中第一次只出现一次的字母"></a>7、找出字符串中第一次只出现一次的字母</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">String.prototype.firstAppear = function () &#123;</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;&#125;,</span><br><span class="line">        len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (obj[<span class="keyword">this</span>[i]]) &#123;</span><br><span class="line">            obj[<span class="keyword">this</span>[i]]++;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            obj[<span class="keyword">this</span>[i]] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> obj) &#123;</span><br><span class="line">       <span class="keyword">if</span> (obj[prop] == <span class="number">1</span>) &#123;</span><br><span class="line">         <span class="keyword">return</span> prop;</span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8、找元素的第n级父元素"><a href="#8、找元素的第n级父元素" class="headerlink" title="8、找元素的第n级父元素"></a>8、找元素的第n级父元素</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">parents</span>(<span class="params">ele, n</span>) &#123;</span><br><span class="line">    <span class="keyword">while</span> (ele &amp;&amp; n) &#123;</span><br><span class="line">        ele = ele.<span class="property">parentElement</span> ? ele.<span class="property">parentElement</span> : ele.<span class="property">parentNode</span>;</span><br><span class="line">        n--;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ele;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9、-返回元素的第n个兄弟节点"><a href="#9、-返回元素的第n个兄弟节点" class="headerlink" title="9、 返回元素的第n个兄弟节点"></a>9、 返回元素的第n个兄弟节点</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">retSibling</span>(<span class="params">e, n</span>) &#123;</span><br><span class="line">    <span class="keyword">while</span> (e &amp;&amp; n) &#123;</span><br><span class="line">        <span class="keyword">if</span> (n &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (e.<span class="property">nextElementSibling</span>) &#123;</span><br><span class="line">                e = e.<span class="property">nextElementSibling</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">for</span> (e = e.<span class="property">nextSibling</span>; e &amp;&amp; e.<span class="property">nodeType</span> !== <span class="number">1</span>; e = e.<span class="property">nextSibling</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            n--;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (e.<span class="property">previousElementSibling</span>) &#123;</span><br><span class="line">                e = e.<span class="property">previousElementSibling</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">for</span> (e = e.<span class="property">previousElementSibling</span>; e &amp;&amp; e.<span class="property">nodeType</span> !== <span class="number">1</span>; e = e.<span class="property">previousElementSibling</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            n++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> e;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="10、封装mychildren，解决浏览器的兼容问题"><a href="#10、封装mychildren，解决浏览器的兼容问题" class="headerlink" title="10、封装mychildren，解决浏览器的兼容问题"></a>10、封装mychildren，解决浏览器的兼容问题</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">myChildren</span>(<span class="params">e</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> children = e.<span class="property">childNodes</span>,</span><br><span class="line">        arr = [],</span><br><span class="line">        len = children.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (children[i].<span class="property">nodeType</span> === <span class="number">1</span>) &#123;</span><br><span class="line">            arr.<span class="title function_">push</span>(children[i])</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="11、判断元素有没有子元素"><a href="#11、判断元素有没有子元素" class="headerlink" title="11、判断元素有没有子元素"></a>11、判断元素有没有子元素</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">hasChildren</span>(<span class="params">e</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> children = e.<span class="property">childNodes</span>,</span><br><span class="line">        len = children.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (children[i].<span class="property">nodeType</span> === <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="12、我一个元素插入到另一个元素的后面"><a href="#12、我一个元素插入到另一个元素的后面" class="headerlink" title="12、我一个元素插入到另一个元素的后面"></a>12、我一个元素插入到另一个元素的后面</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Element</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">insertAfter</span> = <span class="keyword">function</span> (<span class="params">target, elen</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> nextElen = elen.<span class="property">nextElenmentSibling</span>;</span><br><span class="line">    <span class="keyword">if</span> (nextElen == <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">appendChild</span>(target);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">insertBefore</span>(target, nextElen);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="13、返回当前的时间（年月日时分秒）"><a href="#13、返回当前的时间（年月日时分秒）" class="headerlink" title="13、返回当前的时间（年月日时分秒）"></a>13、返回当前的时间（年月日时分秒）</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getDateTime</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="title class_">Date</span>(),</span><br><span class="line">        year = date.<span class="title function_">getFullYear</span>(),</span><br><span class="line">        month = date.<span class="title function_">getMonth</span>() + <span class="number">1</span>,</span><br><span class="line">        day = date.<span class="title function_">getDate</span>(),</span><br><span class="line">        hour = date.<span class="title function_">getHours</span>() + <span class="number">1</span>,</span><br><span class="line">        minute = date.<span class="title function_">getMinutes</span>(),</span><br><span class="line">        second = date.<span class="title function_">getSeconds</span>();</span><br><span class="line">        month = <span class="title function_">checkTime</span>(month);</span><br><span class="line">        day = <span class="title function_">checkTime</span>(day);</span><br><span class="line">        hour = <span class="title function_">checkTime</span>(hour);</span><br><span class="line">        minute = <span class="title function_">checkTime</span>(minute);</span><br><span class="line">        second = <span class="title function_">checkTime</span>(second);</span><br><span class="line">     <span class="keyword">function</span> <span class="title function_">checkTime</span>(<span class="params">i</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (i &lt; <span class="number">10</span>) &#123;</span><br><span class="line">                i = <span class="string">&quot;0&quot;</span> + i;</span><br><span class="line">       &#125;</span><br><span class="line">      <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;&quot;</span> + year + <span class="string">&quot;年&quot;</span> + month + <span class="string">&quot;月&quot;</span> + day + <span class="string">&quot;日&quot;</span> + hour + <span class="string">&quot;时&quot;</span> + minute + <span class="string">&quot;分&quot;</span> + second + <span class="string">&quot;秒&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="14、获得滚动条的滚动距离"><a href="#14、获得滚动条的滚动距离" class="headerlink" title="14、获得滚动条的滚动距离"></a>14、获得滚动条的滚动距离</h3><p> function getScrollOffset() {<br> if (window.pageXOffset) {<br> return {<br> x: window.pageXOffset,<br> y: window.pageYOffset<br> }<br> } else {<br> return {<br> x: document.body.scrollLeft + document.documentElement.scrollLeft,<br> y: document.body.scrollTop + document.documentElement.scrollTop<br> }<br> }<br> }</p>
<h3 id="15、获得视口的尺寸"><a href="#15、获得视口的尺寸" class="headerlink" title="15、获得视口的尺寸"></a>15、获得视口的尺寸</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getViewportOffset</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">innerWidth</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">w</span>: <span class="variable language_">window</span>.<span class="property">innerWidth</span>,</span><br><span class="line">            <span class="attr">h</span>: <span class="variable language_">window</span>.<span class="property">innerHeight</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// ie8及其以下</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">compatMode</span> === <span class="string">&quot;BackCompat&quot;</span>) &#123;</span><br><span class="line">            <span class="comment">// 怪异模式</span></span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                <span class="attr">w</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientWidth</span>,</span><br><span class="line">                <span class="attr">h</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">clientHeight</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 标准模式</span></span><br><span class="line">            <span class="keyword">return</span> &#123;</span><br><span class="line">                <span class="attr">w</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientWidth</span>,</span><br><span class="line">                <span class="attr">h</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="16、获取任一元素的任意属性"><a href="#16、获取任一元素的任意属性" class="headerlink" title="16、获取任一元素的任意属性"></a>16、获取任一元素的任意属性</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getStyle</span>(<span class="params">elem, prop</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">window</span>.<span class="property">getComputedStyle</span> ? <span class="variable language_">window</span>.<span class="title function_">getComputedStyle</span>(elem, <span class="literal">null</span>)[prop] : elem.<span class="property">currentStyle</span>[prop]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="17、绑定事件的兼容代码"><a href="#17、绑定事件的兼容代码" class="headerlink" title="17、绑定事件的兼容代码"></a>17、绑定事件的兼容代码</h3><figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">addEvent</span>(<span class="params">elem, <span class="keyword">type</span>, handle</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (elem.<span class="property">addEventListener</span>) &#123; <span class="comment">//非ie和非ie9</span></span><br><span class="line">        elem.<span class="title function_">addEventListener</span>(<span class="keyword">type</span>, handle, <span class="literal">false</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (elem.<span class="property">attachEvent</span>) &#123; <span class="comment">//ie6到ie8</span></span><br><span class="line">        elem.<span class="title function_">attachEvent</span>(<span class="string">&#x27;on&#x27;</span> + <span class="keyword">type</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            handle.<span class="title function_">call</span>(elem);</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        elem[<span class="string">&#x27;on&#x27;</span> + <span class="keyword">type</span>] = handle;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="18、解绑事件"><a href="#18、解绑事件" class="headerlink" title="18、解绑事件"></a>18、解绑事件</h3><figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">removeEvent</span>(<span class="params">elem, <span class="keyword">type</span>, handle</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (elem.<span class="property">removeEventListener</span>) &#123; <span class="comment">//非ie和非ie9</span></span><br><span class="line">        elem.<span class="title function_">removeEventListener</span>(<span class="keyword">type</span>, handle, <span class="literal">false</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (elem.<span class="property">detachEvent</span>) &#123; <span class="comment">//ie6到ie8</span></span><br><span class="line">        elem.<span class="title function_">detachEvent</span>(<span class="string">&#x27;on&#x27;</span> + <span class="keyword">type</span>, handle);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        elem[<span class="string">&#x27;on&#x27;</span> + <span class="keyword">type</span>] = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="19、取消冒泡的兼容代码"><a href="#19、取消冒泡的兼容代码" class="headerlink" title="19、取消冒泡的兼容代码"></a>19、取消冒泡的兼容代码</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">stopBubble</span>(<span class="params">e</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (e &amp;&amp; e.<span class="property">stopPropagation</span>) &#123;</span><br><span class="line">        e.<span class="title function_">stopPropagation</span>();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">event</span>.<span class="property">cancelBubble</span> = <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="20、检验字符串是否是回文"><a href="#20、检验字符串是否是回文" class="headerlink" title="20、检验字符串是否是回文"></a>20、检验字符串是否是回文</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">isPalina</span>(<span class="params">str</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">toString</span>.<span class="title function_">call</span>(str) !== <span class="string">&#x27;[object String]&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> len = str.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len / <span class="number">2</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (str[i] != str[len - <span class="number">1</span> - i]) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="21、检验字符串是否是回文"><a href="#21、检验字符串是否是回文" class="headerlink" title="21、检验字符串是否是回文"></a>21、检验字符串是否是回文</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">isPalindrome</span>(<span class="params">str</span>) &#123;</span><br><span class="line">    str = str.<span class="title function_">replace</span>(<span class="regexp">/\W/g</span>, <span class="string">&#x27;&#x27;</span>).<span class="title function_">toLowerCase</span>();</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(str)</span><br><span class="line">    <span class="keyword">return</span> (str == str.<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>).<span class="title function_">reverse</span>().<span class="title function_">join</span>(<span class="string">&#x27;&#x27;</span>))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="22、兼容getElementsByClassName方法"><a href="#22、兼容getElementsByClassName方法" class="headerlink" title="22、兼容getElementsByClassName方法"></a>22、兼容getElementsByClassName方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Element</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">getElementsByClassName</span> = <span class="title class_">Document</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">getElementsByClassName</span> = <span class="keyword">function</span> (<span class="params">_className</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> allDomArray = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&#x27;*&#x27;</span>);</span><br><span class="line">    <span class="keyword">var</span> lastDomArray = [];</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">trimSpace</span>(<span class="params">strClass</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> reg = <span class="regexp">/\s+/g</span>;</span><br><span class="line">        <span class="keyword">return</span> strClass.<span class="title function_">replace</span>(reg, <span class="string">&#x27; &#x27;</span>).<span class="title function_">trim</span>()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; allDomArray.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> classArray = <span class="title function_">trimSpace</span>(allDomArray[i].<span class="property">className</span>).<span class="title function_">split</span>(<span class="string">&#x27; &#x27;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; classArray.<span class="property">length</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (classArray[j] == _className) &#123;</span><br><span class="line">                lastDomArray.<span class="title function_">push</span>(allDomArray[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> lastDomArray;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="23、运动函数"><a href="#23、运动函数" class="headerlink" title="23、运动函数"></a>23、运动函数</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">animate</span>(<span class="params">obj, json, callback</span>) &#123;</span><br><span class="line">    <span class="built_in">clearInterval</span>(obj.<span class="property">timer</span>);</span><br><span class="line">    <span class="keyword">var</span> speed,</span><br><span class="line">        current;</span><br><span class="line">    obj.<span class="property">timer</span> = <span class="built_in">setInterval</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> lock = <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> json) &#123;</span><br><span class="line">            <span class="keyword">if</span> (prop == <span class="string">&#x27;opacity&#x27;</span>) &#123;</span><br><span class="line">                current = <span class="built_in">parseFloat</span>(<span class="variable language_">window</span>.<span class="title function_">getComputedStyle</span>(obj, <span class="literal">null</span>)[prop]) * <span class="number">100</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                current = <span class="built_in">parseInt</span>(<span class="variable language_">window</span>.<span class="title function_">getComputedStyle</span>(obj, <span class="literal">null</span>)[prop]);</span><br><span class="line">            &#125;</span><br><span class="line">            speed = (json[prop] - current) / <span class="number">7</span>;</span><br><span class="line">            speed = speed &gt; <span class="number">0</span> ? <span class="title class_">Math</span>.<span class="title function_">ceil</span>(speed) : <span class="title class_">Math</span>.<span class="title function_">floor</span>(speed);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (prop == <span class="string">&#x27;opacity&#x27;</span>) &#123;</span><br><span class="line">                obj.<span class="property">style</span>[prop] = (current + speed) / <span class="number">100</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                obj.<span class="property">style</span>[prop] = current + speed + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (current != json[prop]) &#123;</span><br><span class="line">                lock = <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (lock) &#123;</span><br><span class="line">            <span class="built_in">clearInterval</span>(obj.<span class="property">timer</span>);</span><br><span class="line">            <span class="keyword">typeof</span> callback == <span class="string">&#x27;function&#x27;</span> ? <span class="title function_">callback</span>() : <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;, <span class="number">30</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="24、弹性运动"><a href="#24、弹性运动" class="headerlink" title="24、弹性运动"></a>24、弹性运动</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">ElasticMovement</span>(<span class="params">obj, target</span>) &#123;</span><br><span class="line">    <span class="built_in">clearInterval</span>(obj.<span class="property">timer</span>);</span><br><span class="line">    <span class="keyword">var</span> iSpeed = <span class="number">40</span>,</span><br><span class="line">        a, u = <span class="number">0.8</span>;</span><br><span class="line">    obj.<span class="property">timer</span> = <span class="built_in">setInterval</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        a = (target - obj.<span class="property">offsetLeft</span>) / <span class="number">8</span>;</span><br><span class="line">        iSpeed = iSpeed + a;</span><br><span class="line">        iSpeed = iSpeed * u;</span><br><span class="line">        <span class="keyword">if</span> (<span class="title class_">Math</span>.<span class="title function_">abs</span>(iSpeed) &lt;= <span class="number">1</span> &amp;&amp; <span class="title class_">Math</span>.<span class="title function_">abs</span>(a) &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;over&#x27;</span>)</span><br><span class="line">            <span class="built_in">clearInterval</span>(obj.<span class="property">timer</span>);</span><br><span class="line">            obj.<span class="property">style</span>.<span class="property">left</span> = target + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            obj.<span class="property">style</span>.<span class="property">left</span> = obj.<span class="property">offsetLeft</span> + iSpeed + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;, <span class="number">30</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="25、封装自己的forEach方法"><a href="#25、封装自己的forEach方法" class="headerlink" title="25、封装自己的forEach方法"></a>25、封装自己的forEach方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myForEach</span> = <span class="keyword">function</span> (<span class="params">func, obj</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> len = <span class="variable language_">this</span>.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="variable language_">arguments</span>[<span class="number">1</span>] ? <span class="variable language_">arguments</span>[<span class="number">1</span>] : <span class="variable language_">window</span>;</span><br><span class="line">    <span class="comment">// var _this=arguments[1]||window;</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        func.<span class="title function_">call</span>(_this, <span class="variable language_">this</span>[i], i, <span class="variable language_">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="26、封装自己的filter方法"><a href="#26、封装自己的filter方法" class="headerlink" title="26、封装自己的filter方法"></a>26、封装自己的filter方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myFilter</span> = <span class="keyword">function</span> (<span class="params">func, obj</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> len = <span class="variable language_">this</span>.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="variable language_">arguments</span>[<span class="number">1</span>] || <span class="variable language_">window</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        func.<span class="title function_">call</span>(_this, <span class="variable language_">this</span>[i], i, <span class="variable language_">this</span>) &amp;&amp; arr.<span class="title function_">push</span>(<span class="variable language_">this</span>[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="27、数组map方法"><a href="#27、数组map方法" class="headerlink" title="27、数组map方法"></a>27、数组map方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myMap</span> = <span class="keyword">function</span> (<span class="params">func</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> arr = [];</span><br><span class="line">    <span class="keyword">var</span> len = <span class="variable language_">this</span>.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="variable language_">arguments</span>[<span class="number">1</span>] || <span class="variable language_">window</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        arr.<span class="title function_">push</span>(func.<span class="title function_">call</span>(_this, <span class="variable language_">this</span>[i], i, <span class="variable language_">this</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="28、数组every方法"><a href="#28、数组every方法" class="headerlink" title="28、数组every方法"></a>28、数组every方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myEvery</span> = <span class="keyword">function</span> (<span class="params">func</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> flag = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">var</span> len = <span class="variable language_">this</span>.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">var</span> _this = <span class="variable language_">arguments</span>[<span class="number">1</span>] || <span class="variable language_">window</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (func.<span class="title function_">apply</span>(_this, [<span class="variable language_">this</span>[i], i, <span class="variable language_">this</span>]) == <span class="literal">false</span>) &#123;</span><br><span class="line">            flag = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="29、数组reduce方法"><a href="#29、数组reduce方法" class="headerlink" title="29、数组reduce方法"></a>29、数组reduce方法</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Array.prototype.myReduce = function (func, initialValue) &#123;</span><br><span class="line">    <span class="keyword">var</span> len = <span class="keyword">this</span>.length,</span><br><span class="line">        nextValue,</span><br><span class="line">        i;</span><br><span class="line">    <span class="keyword">if</span> (!initialValue) &#123;</span><br><span class="line">        <span class="comment">// 没有传第二个参数</span></span><br><span class="line">        nextValue = <span class="keyword">this</span>[<span class="number">0</span>];</span><br><span class="line">        i = <span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 传了第二个参数</span></span><br><span class="line">        nextValue = initialValue;</span><br><span class="line">        i = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (; i &lt; len; i++) &#123;</span><br><span class="line">        nextValue = func(nextValue, <span class="keyword">this</span>[i], i, <span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> nextValue;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="30、获取url中的参数"><a href="#30、获取url中的参数" class="headerlink" title="30、获取url中的参数"></a>30、获取url中的参数</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getWindonHref</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> sHref = <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span>;</span><br><span class="line">    <span class="keyword">var</span> args = sHref.<span class="title function_">split</span>(<span class="string">&#x27;?&#x27;</span>);</span><br><span class="line">    <span class="keyword">if</span> (args[<span class="number">0</span>] === sHref) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> hrefarr = args[<span class="number">1</span>].<span class="title function_">split</span>(<span class="string">&#x27;#&#x27;</span>)[<span class="number">0</span>].<span class="title function_">split</span>(<span class="string">&#x27;&amp;&#x27;</span>);</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; hrefarr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        hrefarr[i] = hrefarr[i].<span class="title function_">split</span>(<span class="string">&#x27;=&#x27;</span>);</span><br><span class="line">        obj[hrefarr[i][<span class="number">0</span>]] = hrefarr[i][<span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> obj;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="31、数组排序"><a href="#31、数组排序" class="headerlink" title="31、数组排序"></a>31、数组排序</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 快排 [left] + min + [right]</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">quickArr</span>(<span class="params">arr</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (arr.<span class="property">length</span> &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> arr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> left = [],</span><br><span class="line">        right = [];</span><br><span class="line">    <span class="keyword">var</span> pIndex = <span class="title class_">Math</span>.<span class="title function_">floor</span>(arr.<span class="property">length</span> / <span class="number">2</span>);</span><br><span class="line">    <span class="keyword">var</span> p = arr.<span class="title function_">splice</span>(pIndex, <span class="number">1</span>)[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (arr[i] &lt;= p) &#123;</span><br><span class="line">            left.<span class="title function_">push</span>(arr[i]);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            right.<span class="title function_">push</span>(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 递归</span></span><br><span class="line">    <span class="keyword">return</span> <span class="title function_">quickArr</span>(left).<span class="title function_">concat</span>([p], <span class="title function_">quickArr</span>(right));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 冒泡</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">bubbleSort</span>(<span class="params">arr</span>) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span> - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = i + <span class="number">1</span>; j &lt; arr.<span class="property">length</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (arr[i] &gt; arr[j]) &#123;</span><br><span class="line">                <span class="keyword">var</span> temp = arr[i];</span><br><span class="line">                arr[i] = arr[j];</span><br><span class="line">                arr[j] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">bubbleSort</span>(<span class="params">arr</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> len = arr.<span class="property">length</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; len - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; len - <span class="number">1</span> - i; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (arr[j] &gt; arr[j + <span class="number">1</span>]) &#123;</span><br><span class="line">                <span class="keyword">var</span> temp = arr[j];</span><br><span class="line">                arr[j] = arr[j + <span class="number">1</span>];</span><br><span class="line">                arr[j + <span class="number">1</span>] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="32、遍历Dom树"><a href="#32、遍历Dom树" class="headerlink" title="32、遍历Dom树"></a>32、遍历Dom树</h3><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 给定页面上的DOM元素,将访问元素本身及其所有后代(不仅仅是它的直接子元素)</span></span><br><span class="line"><span class="comment">// 对于每个访问的元素，函数讲元素传递给提供的回调函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">traverse</span>(<span class="params">element, callback</span>) </span>&#123;</span><br><span class="line">    <span class="title function_ invoke__">callback</span>(element);</span><br><span class="line">    <span class="keyword">var</span> <span class="keyword">list</span> = element.children;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="keyword">list</span>.length; i++) &#123;</span><br><span class="line">        <span class="title function_ invoke__">traverse</span>(<span class="keyword">list</span>[i], callback);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="33、原生js封装ajax"><a href="#33、原生js封装ajax" class="headerlink" title="33、原生js封装ajax"></a>33、原生js封装ajax</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">ajax</span>(<span class="params">method, url, callback, data, flag</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> xhr;</span><br><span class="line">    flag = flag || <span class="literal">true</span>;</span><br><span class="line">    method = method.<span class="title function_">toUpperCase</span>();</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">        xhr = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        xhr = <span class="keyword">new</span> <span class="title class_">ActiveXObject</span>(<span class="string">&#x27;Microsoft.XMLHttp&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    xhr.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (xhr.<span class="property">readyState</span> == <span class="number">4</span> &amp;&amp; xhr.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">2</span>)</span><br><span class="line">            <span class="title function_">callback</span>(xhr.<span class="property">responseText</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (method == <span class="string">&#x27;GET&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="title class_">Date</span>(),</span><br><span class="line">        timer = date.<span class="title function_">getTime</span>();</span><br><span class="line">        xhr.<span class="title function_">open</span>(<span class="string">&#x27;GET&#x27;</span>, url + <span class="string">&#x27;?&#x27;</span> + data + <span class="string">&#x27;&amp;timer&#x27;</span> + timer, flag);</span><br><span class="line">        xhr.<span class="title function_">send</span>()</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method == <span class="string">&#x27;POST&#x27;</span>) &#123;</span><br><span class="line">        xhr.<span class="title function_">open</span>(<span class="string">&#x27;POST&#x27;</span>, url, flag);</span><br><span class="line">        xhr.<span class="title function_">setRequestHeader</span>(<span class="string">&#x27;Content-Type&#x27;</span>, <span class="string">&#x27;application/x-www-form-urlencoded&#x27;</span>);</span><br><span class="line">        xhr.<span class="title function_">send</span>(data);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="34、异步加载script"><a href="#34、异步加载script" class="headerlink" title="34、异步加载script"></a>34、异步加载script</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">loadScript</span>(<span class="params">url, callback</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> oscript = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;script&#x27;</span>);</span><br><span class="line">    <span class="keyword">if</span> (oscript.<span class="property">readyState</span>) &#123; <span class="comment">// ie8及以下版本</span></span><br><span class="line">        oscript.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (oscript.<span class="property">readyState</span> === <span class="string">&#x27;complete&#x27;</span> || oscript.<span class="property">readyState</span> === <span class="string">&#x27;loaded&#x27;</span>) &#123;</span><br><span class="line">                <span class="title function_">callback</span>();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        oscript.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="title function_">callback</span>()</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    oscript.<span class="property">src</span> = url;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(oscript);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="35、cookie管理"><a href="#35、cookie管理" class="headerlink" title="35、cookie管理"></a>35、cookie管理</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> cookie = &#123;</span><br><span class="line">    <span class="attr">set</span>: <span class="keyword">function</span> (<span class="params">name, value, time</span>) &#123;</span><br><span class="line">        <span class="variable language_">document</span>.<span class="property">cookie</span> = name + <span class="string">&#x27;=&#x27;</span> + value + <span class="string">&#x27;; max-age=&#x27;</span> + time;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">remove</span>: <span class="keyword">function</span> (<span class="params">name</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">setCookie</span>(name, <span class="string">&#x27;&#x27;</span>, -<span class="number">1</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">get</span>: <span class="keyword">function</span> (<span class="params">name, callback</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> allCookieArr = <span class="variable language_">document</span>.<span class="property">cookie</span>.<span class="title function_">split</span>(<span class="string">&#x27;; &#x27;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; allCookieArr.<span class="property">length</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">var</span> itemCookieArr = allCookieArr[i].<span class="title function_">split</span>(<span class="string">&#x27;=&#x27;</span>);</span><br><span class="line">            <span class="keyword">if</span> (itemCookieArr[<span class="number">0</span>] === name) &#123;</span><br><span class="line">                <span class="keyword">return</span> itemCookieArr[<span class="number">1</span>]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">undefined</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="36、实现bind-方法"><a href="#36、实现bind-方法" class="headerlink" title="36、实现bind()方法"></a>36、实现bind()方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myBind</span> = <span class="keyword">function</span> (<span class="params">target</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> target = target || <span class="variable language_">window</span>;</span><br><span class="line">    <span class="keyword">var</span> _args1 = [].<span class="property">slice</span>.<span class="title function_">call</span>(<span class="variable language_">arguments</span>, <span class="number">1</span>);</span><br><span class="line">    <span class="keyword">var</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="keyword">var</span> temp = <span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;;</span><br><span class="line">    <span class="keyword">var</span> F = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> _args2 = [].<span class="property">slice</span>.<span class="title function_">call</span>(<span class="variable language_">arguments</span>, <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">var</span> parasArr = _args1.<span class="title function_">concat</span>(_args2);</span><br><span class="line">        <span class="keyword">return</span> self.<span class="title function_">apply</span>(<span class="variable language_">this</span> <span class="keyword">instanceof</span> temp ? <span class="variable language_">this</span> : target, parasArr)</span><br><span class="line">    &#125;</span><br><span class="line">    temp.<span class="property"><span class="keyword">prototype</span></span> = self.<span class="property"><span class="keyword">prototype</span></span>;</span><br><span class="line">    F.<span class="property"><span class="keyword">prototype</span></span> = <span class="keyword">new</span> <span class="title function_">temp</span>();</span><br><span class="line">    <span class="keyword">return</span> F;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="37、实现call-方法"><a href="#37、实现call-方法" class="headerlink" title="37、实现call()方法"></a>37、实现call()方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myCall</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> ctx = <span class="variable language_">arguments</span>[<span class="number">0</span>] || <span class="variable language_">window</span>;</span><br><span class="line">    ctx.<span class="property">fn</span> = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="keyword">var</span> args = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; <span class="variable language_">arguments</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        args.<span class="title function_">push</span>(<span class="variable language_">arguments</span>[i])</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> result = ctx.<span class="title function_">fn</span>(...args);</span><br><span class="line">    <span class="keyword">delete</span> ctx.<span class="property">fn</span>;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="38、实现apply-方法"><a href="#38、实现apply-方法" class="headerlink" title="38、实现apply()方法"></a>38、实现apply()方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">myApply</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> ctx = <span class="variable language_">arguments</span>[<span class="number">0</span>] || <span class="variable language_">window</span>;</span><br><span class="line">    ctx.<span class="property">fn</span> = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="variable language_">arguments</span>[<span class="number">1</span>]) &#123;</span><br><span class="line">        <span class="keyword">var</span> result = ctx.<span class="title function_">fn</span>();</span><br><span class="line">        <span class="keyword">delete</span> ctx.<span class="property">fn</span>;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> result = ctx.<span class="title function_">fn</span>(...<span class="variable language_">arguments</span>[<span class="number">1</span>]);</span><br><span class="line">    <span class="keyword">delete</span> ctx.<span class="property">fn</span>;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="39、防抖"><a href="#39、防抖" class="headerlink" title="39、防抖"></a>39、防抖</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">debounce</span>(<span class="params">handle, delay</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> timer = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> _self = <span class="variable language_">this</span>,</span><br><span class="line">            _args = <span class="variable language_">arguments</span>;</span><br><span class="line">        <span class="built_in">clearTimeout</span>(timer);</span><br><span class="line">        timer = <span class="built_in">setTimeout</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            handle.<span class="title function_">apply</span>(_self, _args)</span><br><span class="line">        &#125;, delay)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="40、节流"><a href="#40、节流" class="headerlink" title="40、节流"></a>40、节流</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">throttle</span>(<span class="params">handler, wait</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> lastTime = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span> (<span class="params">e</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> nowTime = <span class="keyword">new</span> <span class="title class_">Date</span>().<span class="title function_">getTime</span>();</span><br><span class="line">        <span class="keyword">if</span> (nowTime - lastTime &gt; wait) &#123;</span><br><span class="line">            handler.<span class="title function_">apply</span>(<span class="variable language_">this</span>, <span class="variable language_">arguments</span>);</span><br><span class="line">            lastTime = nowTime;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="41、requestAnimFrame兼容性方法"><a href="#41、requestAnimFrame兼容性方法" class="headerlink" title="41、requestAnimFrame兼容性方法"></a>41、requestAnimFrame兼容性方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">requestAnimFrame</span> = (<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">window</span>.<span class="property">requestAnimationFrame</span> ||</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">webkitRequestAnimationFrame</span> ||</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">mozRequestAnimationFrame</span> ||</span><br><span class="line">        <span class="keyword">function</span> (<span class="params">callback</span>) &#123;</span><br><span class="line">            <span class="variable language_">window</span>.<span class="built_in">setTimeout</span>(callback, <span class="number">1000</span> / <span class="number">60</span>);</span><br><span class="line">        &#125;;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<h3 id="42、cancelAnimFrame兼容性方法"><a href="#42、cancelAnimFrame兼容性方法" class="headerlink" title="42、cancelAnimFrame兼容性方法"></a>42、cancelAnimFrame兼容性方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">cancelAnimFrame</span> = (<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">window</span>.<span class="property">cancelAnimationFrame</span> ||</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">webkitCancelAnimationFrame</span> ||</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">mozCancelAnimationFrame</span> ||</span><br><span class="line">        <span class="keyword">function</span> (<span class="params">id</span>) &#123;</span><br><span class="line">            <span class="variable language_">window</span>.<span class="built_in">clearTimeout</span>(id);</span><br><span class="line">        &#125;;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<h3 id="43、jsonp底层方法"><a href="#43、jsonp底层方法" class="headerlink" title="43、jsonp底层方法"></a>43、jsonp底层方法</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">jsonp</span>(<span class="params">url, callback</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> oscript = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&#x27;script&#x27;</span>);</span><br><span class="line">    <span class="keyword">if</span> (oscript.<span class="property">readyState</span>) &#123; <span class="comment">// ie8及以下版本</span></span><br><span class="line">        oscript.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (oscript.<span class="property">readyState</span> === <span class="string">&#x27;complete&#x27;</span> || oscript.<span class="property">readyState</span> === <span class="string">&#x27;loaded&#x27;</span>) &#123;</span><br><span class="line">                <span class="title function_">callback</span>();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        oscript.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="title function_">callback</span>()</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    oscript.<span class="property">src</span> = url;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="property">body</span>.<span class="title function_">appendChild</span>(oscript);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="44、获取url上的参数"><a href="#44、获取url上的参数" class="headerlink" title="44、获取url上的参数"></a>44、获取url上的参数</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getUrlParam</span>(<span class="params">sUrl, sKey</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> result = &#123;&#125;;</span><br><span class="line">    sUrl.<span class="title function_">replace</span>(<span class="regexp">/(\w+)=(\w+)(?=[&amp;|#])/g</span>, <span class="keyword">function</span> (<span class="params">ele, key, val</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!result[key]) &#123;</span><br><span class="line">            result[key] = val;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">var</span> temp = result[key];</span><br><span class="line">            result[key] = [].<span class="title function_">concat</span>(temp, val);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">if</span> (!sKey) &#123;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> result[sKey] || <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="45、格式化时间"><a href="#45、格式化时间" class="headerlink" title="45、格式化时间"></a>45、格式化时间</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">formatDate</span>(<span class="params">t, str</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;</span><br><span class="line">        <span class="attr">yyyy</span>: t.<span class="title function_">getFullYear</span>(),</span><br><span class="line">        <span class="attr">yy</span>: (<span class="string">&quot;&quot;</span> + t.<span class="title function_">getFullYear</span>()).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">M</span>: t.<span class="title function_">getMonth</span>() + <span class="number">1</span>,</span><br><span class="line">        <span class="attr">MM</span>: (<span class="string">&quot;0&quot;</span> + (t.<span class="title function_">getMonth</span>() + <span class="number">1</span>)).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">d</span>: t.<span class="title function_">getDate</span>(),</span><br><span class="line">        <span class="attr">dd</span>: (<span class="string">&quot;0&quot;</span> + t.<span class="title function_">getDate</span>()).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">H</span>: t.<span class="title function_">getHours</span>(),</span><br><span class="line">        <span class="attr">HH</span>: (<span class="string">&quot;0&quot;</span> + t.<span class="title function_">getHours</span>()).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">h</span>: t.<span class="title function_">getHours</span>() % <span class="number">12</span>,</span><br><span class="line">        <span class="attr">hh</span>: (<span class="string">&quot;0&quot;</span> + t.<span class="title function_">getHours</span>() % <span class="number">12</span>).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">m</span>: t.<span class="title function_">getMinutes</span>(),</span><br><span class="line">        <span class="attr">mm</span>: (<span class="string">&quot;0&quot;</span> + t.<span class="title function_">getMinutes</span>()).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">s</span>: t.<span class="title function_">getSeconds</span>(),</span><br><span class="line">        <span class="attr">ss</span>: (<span class="string">&quot;0&quot;</span> + t.<span class="title function_">getSeconds</span>()).<span class="title function_">slice</span>(-<span class="number">2</span>),</span><br><span class="line">        <span class="attr">w</span>: [<span class="string">&#x27;日&#x27;</span>, <span class="string">&#x27;一&#x27;</span>, <span class="string">&#x27;二&#x27;</span>, <span class="string">&#x27;三&#x27;</span>, <span class="string">&#x27;四&#x27;</span>, <span class="string">&#x27;五&#x27;</span>, <span class="string">&#x27;六&#x27;</span>][t.<span class="title function_">getDay</span>()]</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">return</span> str.<span class="title function_">replace</span>(<span class="regexp">/([a-z]+)/ig</span>, <span class="keyword">function</span> (<span class="params">$<span class="number">1</span></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> obj[$<span class="number">1</span>]</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="46、验证邮箱的正则表达式"><a href="#46、验证邮箱的正则表达式" class="headerlink" title="46、验证邮箱的正则表达式"></a>46、验证邮箱的正则表达式</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">isAvailableEmail</span>(<span class="params">sEmail</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> reg = <span class="regexp">/^([\w+\.])+@\w+([.]\w+)+$/</span></span><br><span class="line">    <span class="keyword">return</span> reg.<span class="title function_">test</span>(sEmail)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="47、函数柯里化"><a href="#47、函数柯里化" class="headerlink" title="47、函数柯里化"></a>47、函数柯里化</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数，并且返回接受余下的参数且返回结果的新函数的技术</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">curryIt</span>(<span class="params">fn</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> length = fn.<span class="property">length</span>,</span><br><span class="line">        args = [];</span><br><span class="line">    <span class="keyword">var</span> result = <span class="keyword">function</span> (<span class="params">arg</span>) &#123;</span><br><span class="line">        args.<span class="title function_">push</span>(arg);</span><br><span class="line">        length--;</span><br><span class="line">        <span class="keyword">if</span> (length &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> fn.<span class="title function_">apply</span>(<span class="variable language_">this</span>, args);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> result;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="48、大数相加"><a href="#48、大数相加" class="headerlink" title="48、大数相加"></a>48、大数相加</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">sumBigNumber</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> res = <span class="string">&#x27;&#x27;</span>, <span class="comment">//结果</span></span><br><span class="line">        temp = <span class="number">0</span>; <span class="comment">//按位加的结果及进位</span></span><br><span class="line">    a = a.<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">    b = b.<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">    <span class="keyword">while</span> (a.<span class="property">length</span> || b.<span class="property">length</span> || temp) &#123;</span><br><span class="line">        <span class="comment">//~~按位非 1.类型转换，转换成数字 2.~~undefined==0 </span></span><br><span class="line">        temp += ~~a.<span class="title function_">pop</span>() + ~~b.<span class="title function_">pop</span>();</span><br><span class="line">        res = (temp % <span class="number">10</span>) + res;</span><br><span class="line">        temp = temp &gt; <span class="number">9</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res.<span class="title function_">replace</span>(<span class="regexp">/^0+/</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="49、单例模式"><a href="#49、单例模式" class="headerlink" title="49、单例模式"></a>49、单例模式</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">getSingle</span>(<span class="params">func</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> result;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!result) &#123;</span><br><span class="line">            result = <span class="keyword">new</span> <span class="title function_">func</span>(<span class="variable language_">arguments</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="1、数组去重"><a href="#1、数组去重" class="headerlink" title="1、数组去重"></a>1、数组去重</h1><h2 id="1、from-叠加new-Set-方法"><a href="#1、from-叠加new-Set-方法" class="headerlink" title="1、from()叠加new Set()方法"></a>1、from()叠加new Set()方法</h2><p>字符串或数值型数组的去重可以直接使用from方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> uniquePlants = <span class="title class_">Array</span>.<span class="title function_">from</span>(<span class="keyword">new</span> <span class="title class_">Set</span>(plants)); </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(uniquePlants); <span class="comment">// [ &#x27;Saturn&#x27;, &#x27;Earth&#x27;, &#x27;Uranus&#x27;, &#x27;Mercury&#x27;, &#x27;Venus&#x27;, &#x27;Mars&#x27;, &#x27;Jupiter&#x27; ]</span></span><br></pre></td></tr></table></figure>

<h2 id="2、spread操作符-…"><a href="#2、spread操作符-…" class="headerlink" title="2、spread操作符(…)"></a>2、spread操作符(…)</h2><p>扩展运算符是ES6的一大创新，还有很多强大的功能。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> uniquePlants = [...<span class="keyword">new</span> <span class="title class_">Set</span>(plants)]; </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(uniquePlants); <span class="comment">// [ &#x27;Saturn&#x27;, &#x27;Earth&#x27;, &#x27;Uranus&#x27;, &#x27;Mercury&#x27;, &#x27;Venus&#x27;, &#x27;Mars&#x27;, &#x27;Jupiter&#x27; ]</span></span><br></pre></td></tr></table></figure>

<h1 id="2、替换数组中的特定值"><a href="#2、替换数组中的特定值" class="headerlink" title="2、替换数组中的特定值"></a>2、替换数组中的特定值</h1><p>splice() 方法向&#x2F;从数组中添加&#x2F;删除项目，然后返回被删除的项目。<strong>该方法会改变原始数组</strong>。<em>特别需要注意插入值的位置！</em></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// arrayObject.splice(index,howmany,item1,.....,itemX)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> result = plants.<span class="title function_">splice</span>(<span class="number">2</span>, <span class="number">1</span>, <span class="string">&#x27;www.shanzhonglei.com&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plants); <span class="comment">// [&#x27;Saturn&#x27;,&#x27;Uranus&#x27;,&#x27;www.shanzhonglei.com&#x27;,&#x27;Mercury&#x27;,&#x27;Venus&#x27;,&#x27;Earth&#x27;,&#x27;Mars&#x27;,&#x27;Jupiter&#x27;]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">// [&#x27;Mercury&#x27;]</span></span><br></pre></td></tr></table></figure>

<h1 id="3、没有map-的映射数组"><a href="#3、没有map-的映射数组" class="headerlink" title="3、没有map()的映射数组"></a>3、没有map()的映射数组</h1><p>我们先介绍一下map方法。map()方法返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值，它会按照原始数组元素顺序依次处理元素。<strong>注意： map()不会改变原始数组，也不会对空数组进行检测</strong>。<br> 下面我们来实现一个没有map的数组映射：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// array.map(function(currentValue,index,arr), thisValue)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> plants = [</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&quot;Saturn&quot;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&quot;Uranus&quot;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&quot;Mercury&quot;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">&quot;Venus&quot;</span> &#125;,</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> plantsName = <span class="title class_">Array</span>.<span class="title function_">from</span>(plants, <span class="function">(<span class="params">&#123; name &#125;</span>) =&gt;</span> name);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plantsName); <span class="comment">// [ &#x27;Saturn&#x27;, &#x27;Uranus&#x27;, &#x27;Mercury&#x27;, &#x27;Venus&#x27; ]</span></span><br></pre></td></tr></table></figure>

<h1 id="4、空数组"><a href="#4、空数组" class="headerlink" title="4、空数组"></a>4、空数组</h1><p>如果要清空一个数组，将数组的长度设置为0即可，额，这个有点简单。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line">plants.<span class="property">length</span> = <span class="number">0</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plants); <span class="comment">// []</span></span><br></pre></td></tr></table></figure>

<h1 id="5、将数组转换为对象"><a href="#5、将数组转换为对象" class="headerlink" title="5、将数组转换为对象"></a>5、将数组转换为对象</h1><p>如果要将数组转换为对象，最快的方法莫过于spread运算符(…)。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> plantsObj = &#123;...plants &#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plantsObj); <span class="comment">// &#123;&#x27;0&#x27;: &#x27;Saturn&#x27;,&#x27;1&#x27;: &#x27;Earth&#x27;, &#x27;2&#x27;: &#x27;Uranus&#x27;,&#x27;3&#x27;: &#x27;Mercury&#x27;,&#x27;4&#x27;: &#x27;Venus&#x27;,&#x27;5&#x27;: &#x27;Earth&#x27;,&#x27;6&#x27;: &#x27;Mars&#x27;,&#x27;7&#x27;: &#x27;Jupiter&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<h1 id="6、用数据填充数组"><a href="#6、用数据填充数组" class="headerlink" title="6、用数据填充数组"></a>6、用数据填充数组</h1><p>如果我们需要用一些数据来填充数组，或者需要一个具有相同值的数据，我们可以用fill()方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = <span class="keyword">new</span> <span class="title class_">Array</span>(<span class="number">8</span>).<span class="title function_">fill</span>(<span class="string">&#x27;8&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plants); <span class="comment">// [&#x27;8&#x27;, &#x27;8&#x27;, &#x27;8&#x27;,&#x27;8&#x27;, &#x27;8&#x27;, &#x27;8&#x27;,&#x27;8&#x27;, &#x27;8&#x27;]</span></span><br></pre></td></tr></table></figure>

<h1 id="7、合并数组"><a href="#7、合并数组" class="headerlink" title="7、合并数组"></a>7、合并数组</h1><p>当然你会想到concat()方法，但是哦，spread操作符(…)也很香的，这也是扩展运算符的另一个应用。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants1 = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> plants2 = [<span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>([...plants1, ...plants2]); <span class="comment">// [&#x27;Saturn&#x27;, &#x27;Earth&#x27;,&#x27;Uranus&#x27;, &#x27;Mercury&#x27;,&#x27;Venus&#x27;, &#x27;Earth&#x27;,&#x27;Mars&#x27;, &#x27;Jupiter&#x27;]</span></span><br></pre></td></tr></table></figure>

<h1 id="8、两个数组的交集"><a href="#8、两个数组的交集" class="headerlink" title="8、两个数组的交集"></a>8、两个数组的交集</h1><p>要求两个数组的交集，首先确保数组不重复，然后使用filter()方法和includes()方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants1 = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> plants2 = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>];</span><br><span class="line"><span class="keyword">var</span> alonePlants = [...<span class="keyword">new</span> <span class="title class_">Set</span>(plants1)].<span class="title function_">filter</span>(<span class="function"><span class="params">item</span> =&gt;</span> plants2.<span class="title function_">includes</span>(item));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(alonePlants); <span class="comment">// [ &#x27;Saturn&#x27;, &#x27;Earth&#x27;, &#x27;Uranus&#x27; ]</span></span><br></pre></td></tr></table></figure>

<h1 id="9、删除数组中的假值"><a href="#9、删除数组中的假值" class="headerlink" title="9、删除数组中的假值"></a>9、删除数组中的假值</h1><p>我们时常需要在处理数据的时候要去掉假值。<strong>在Javascript中，假值是false, 0， “ “， null, NaN, undefined</strong>。</p>
<figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, null, undefined, <span class="literal">false</span>, <span class="string">&quot;&quot;</span>, NaN, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line">var trueArr = plants.filter(Boolean);</span><br><span class="line">console.log(trueArr); <span class="regexp">//</span> [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>,<span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>,<span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>,<span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>]</span><br></pre></td></tr></table></figure>

<h1 id="10、获取数组中的随机值"><a href="#10、获取数组中的随机值" class="headerlink" title="10、获取数组中的随机值"></a>10、获取数组中的随机值</h1><p>我们可以根据数组长度获得一个随机索引号。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plants[<span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * (plants.<span class="property">length</span> + <span class="number">1</span>))])</span><br></pre></td></tr></table></figure>

<h1 id="11、lastIndexOf-方法"><a href="#11、lastIndexOf-方法" class="headerlink" title="11、lastIndexOf()方法"></a>11、lastIndexOf()方法</h1><p>lastIndexOf()可以帮助我们查找元素最后一次出现的索引。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> plants = [<span class="string">&#x27;Saturn&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Uranus&#x27;</span>, <span class="string">&#x27;Mercury&#x27;</span>, <span class="string">&#x27;Venus&#x27;</span>, <span class="string">&#x27;Earth&#x27;</span>, <span class="string">&#x27;Mars&#x27;</span>, <span class="string">&#x27;Jupiter&#x27;</span>];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(plants.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;Earth&#x27;</span>)) <span class="comment">// 5</span></span><br></pre></td></tr></table></figure>

<h1 id="12、将数组中的所有值相加"><a href="#12、将数组中的所有值相加" class="headerlink" title="12、将数组中的所有值相加"></a>12、将数组中的所有值相加</h1><p>reduce()方法接收一个函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// array.reduce(function(total, currentValue, currentIndex, arr), initialValue)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> nums = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> sum = nums.<span class="title function_">reduce</span>(<span class="function">(<span class="params">x, y</span>) =&gt;</span> x + y);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(sum); <span class="comment">// 15</span></span><br></pre></td></tr></table></figure>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://szhihao.gitee.io">孙志豪</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E7%AE%80%E4%B9%A6%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/">https://szhihao.gitee.io/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E7%AE%80%E4%B9%A6%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="/img/%E5%A4%A9%E7%A9%BA.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E6%9C%80%E8%AF%A6%E7%BB%86%E7%9A%84%E6%89%8B%E5%86%99Promise%E6%95%99%E7%A8%8B/"><img class="prev-cover" src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">史上最最最详细的手写Promise教程</div></div></a></div><div class="next-post pull-right"><a href="/2022/10/11/11-%E5%89%8D%E7%AB%AF%EF%BC%88CSS%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/20+%20css%E9%AB%98%E9%A2%91%E5%AE%9E%E7%94%A8%E7%89%87%E6%AE%B5%EF%BC%8C%E6%8F%90%E9%AB%98%E5%B9%B8%E7%A6%8F%E6%84%9F%E7%9A%84%E5%B0%8F%E6%8A%80%E8%83%BD%E4%BD%A0%E5%8F%AF%E4%BB%A5%E6%8B%A5%E6%9C%89%E5%99%A2/"><img class="next-cover" src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">css高频实用片段，提高幸福感的小技能你可以拥有噢</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/img/%E5%A4%B4%E5%83%8F.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">孙志豪</div><div class="author-info__description">吾日三省吾身：为人谋而不忠乎？与朋友交而不信乎？传不习乎？</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">44</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">15</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/szhihao/szhihao"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/szhihao/szhihao" target="_blank" title="Gitee"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/qq_54753561" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="mailto:zhihaomail@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AE%80%E4%B9%A6%E5%87%BD%E6%95%B0%E5%B0%81%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">简书函数封装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E8%BE%93%E5%85%A5%E4%B8%80%E4%B8%AA%E5%80%BC%EF%BC%8C%E8%BF%94%E5%9B%9E%E5%85%B6%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.0.1.</span> <span class="toc-text">1、输入一个值，返回其数据类型**</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D"><span class="toc-number">1.0.2.</span> <span class="toc-text">2、数组去重</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8E%BB%E9%87%8D"><span class="toc-number">1.0.3.</span> <span class="toc-text">3、字符串去重</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81%E6%B7%B1%E6%8B%B7%E8%B4%9D-%E6%B5%85%E6%8B%B7%E8%B4%9D"><span class="toc-number">1.0.4.</span> <span class="toc-text">4、深拷贝 浅拷贝</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%E3%80%81reverse%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86%E5%92%8C%E6%89%A9%E5%B1%95"><span class="toc-number">1.0.5.</span> <span class="toc-text">5、reverse底层原理和扩展</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6%E3%80%81%E5%9C%A3%E6%9D%AF%E6%A8%A1%E5%BC%8F%E7%9A%84%E7%BB%A7%E6%89%BF"><span class="toc-number">1.0.6.</span> <span class="toc-text">6、圣杯模式的继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7%E3%80%81%E6%89%BE%E5%87%BA%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%8F%AA%E5%87%BA%E7%8E%B0%E4%B8%80%E6%AC%A1%E7%9A%84%E5%AD%97%E6%AF%8D"><span class="toc-number">1.0.7.</span> <span class="toc-text">7、找出字符串中第一次只出现一次的字母</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8%E3%80%81%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%ACn%E7%BA%A7%E7%88%B6%E5%85%83%E7%B4%A0"><span class="toc-number">1.0.8.</span> <span class="toc-text">8、找元素的第n级父元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9%E3%80%81-%E8%BF%94%E5%9B%9E%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%ACn%E4%B8%AA%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9"><span class="toc-number">1.0.9.</span> <span class="toc-text">9、 返回元素的第n个兄弟节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10%E3%80%81%E5%B0%81%E8%A3%85mychildren%EF%BC%8C%E8%A7%A3%E5%86%B3%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98"><span class="toc-number">1.0.10.</span> <span class="toc-text">10、封装mychildren，解决浏览器的兼容问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11%E3%80%81%E5%88%A4%E6%96%AD%E5%85%83%E7%B4%A0%E6%9C%89%E6%B2%A1%E6%9C%89%E5%AD%90%E5%85%83%E7%B4%A0"><span class="toc-number">1.0.11.</span> <span class="toc-text">11、判断元素有没有子元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12%E3%80%81%E6%88%91%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E6%8F%92%E5%85%A5%E5%88%B0%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E7%9A%84%E5%90%8E%E9%9D%A2"><span class="toc-number">1.0.12.</span> <span class="toc-text">12、我一个元素插入到另一个元素的后面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#13%E3%80%81%E8%BF%94%E5%9B%9E%E5%BD%93%E5%89%8D%E7%9A%84%E6%97%B6%E9%97%B4%EF%BC%88%E5%B9%B4%E6%9C%88%E6%97%A5%E6%97%B6%E5%88%86%E7%A7%92%EF%BC%89"><span class="toc-number">1.0.13.</span> <span class="toc-text">13、返回当前的时间（年月日时分秒）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#14%E3%80%81%E8%8E%B7%E5%BE%97%E6%BB%9A%E5%8A%A8%E6%9D%A1%E7%9A%84%E6%BB%9A%E5%8A%A8%E8%B7%9D%E7%A6%BB"><span class="toc-number">1.0.14.</span> <span class="toc-text">14、获得滚动条的滚动距离</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#15%E3%80%81%E8%8E%B7%E5%BE%97%E8%A7%86%E5%8F%A3%E7%9A%84%E5%B0%BA%E5%AF%B8"><span class="toc-number">1.0.15.</span> <span class="toc-text">15、获得视口的尺寸</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#16%E3%80%81%E8%8E%B7%E5%8F%96%E4%BB%BB%E4%B8%80%E5%85%83%E7%B4%A0%E7%9A%84%E4%BB%BB%E6%84%8F%E5%B1%9E%E6%80%A7"><span class="toc-number">1.0.16.</span> <span class="toc-text">16、获取任一元素的任意属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#17%E3%80%81%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6%E7%9A%84%E5%85%BC%E5%AE%B9%E4%BB%A3%E7%A0%81"><span class="toc-number">1.0.17.</span> <span class="toc-text">17、绑定事件的兼容代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#18%E3%80%81%E8%A7%A3%E7%BB%91%E4%BA%8B%E4%BB%B6"><span class="toc-number">1.0.18.</span> <span class="toc-text">18、解绑事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#19%E3%80%81%E5%8F%96%E6%B6%88%E5%86%92%E6%B3%A1%E7%9A%84%E5%85%BC%E5%AE%B9%E4%BB%A3%E7%A0%81"><span class="toc-number">1.0.19.</span> <span class="toc-text">19、取消冒泡的兼容代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#20%E3%80%81%E6%A3%80%E9%AA%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%98%AF%E5%9B%9E%E6%96%87"><span class="toc-number">1.0.20.</span> <span class="toc-text">20、检验字符串是否是回文</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#21%E3%80%81%E6%A3%80%E9%AA%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%98%AF%E5%9B%9E%E6%96%87"><span class="toc-number">1.0.21.</span> <span class="toc-text">21、检验字符串是否是回文</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#22%E3%80%81%E5%85%BC%E5%AE%B9getElementsByClassName%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.22.</span> <span class="toc-text">22、兼容getElementsByClassName方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#23%E3%80%81%E8%BF%90%E5%8A%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.23.</span> <span class="toc-text">23、运动函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#24%E3%80%81%E5%BC%B9%E6%80%A7%E8%BF%90%E5%8A%A8"><span class="toc-number">1.0.24.</span> <span class="toc-text">24、弹性运动</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#25%E3%80%81%E5%B0%81%E8%A3%85%E8%87%AA%E5%B7%B1%E7%9A%84forEach%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.25.</span> <span class="toc-text">25、封装自己的forEach方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#26%E3%80%81%E5%B0%81%E8%A3%85%E8%87%AA%E5%B7%B1%E7%9A%84filter%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.26.</span> <span class="toc-text">26、封装自己的filter方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#27%E3%80%81%E6%95%B0%E7%BB%84map%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.27.</span> <span class="toc-text">27、数组map方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#28%E3%80%81%E6%95%B0%E7%BB%84every%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.28.</span> <span class="toc-text">28、数组every方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#29%E3%80%81%E6%95%B0%E7%BB%84reduce%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.29.</span> <span class="toc-text">29、数组reduce方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#30%E3%80%81%E8%8E%B7%E5%8F%96url%E4%B8%AD%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">1.0.30.</span> <span class="toc-text">30、获取url中的参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#31%E3%80%81%E6%95%B0%E7%BB%84%E6%8E%92%E5%BA%8F"><span class="toc-number">1.0.31.</span> <span class="toc-text">31、数组排序</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#32%E3%80%81%E9%81%8D%E5%8E%86Dom%E6%A0%91"><span class="toc-number">1.0.32.</span> <span class="toc-text">32、遍历Dom树</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#33%E3%80%81%E5%8E%9F%E7%94%9Fjs%E5%B0%81%E8%A3%85ajax"><span class="toc-number">1.0.33.</span> <span class="toc-text">33、原生js封装ajax</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#34%E3%80%81%E5%BC%82%E6%AD%A5%E5%8A%A0%E8%BD%BDscript"><span class="toc-number">1.0.34.</span> <span class="toc-text">34、异步加载script</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#35%E3%80%81cookie%E7%AE%A1%E7%90%86"><span class="toc-number">1.0.35.</span> <span class="toc-text">35、cookie管理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#36%E3%80%81%E5%AE%9E%E7%8E%B0bind-%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.36.</span> <span class="toc-text">36、实现bind()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#37%E3%80%81%E5%AE%9E%E7%8E%B0call-%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.37.</span> <span class="toc-text">37、实现call()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#38%E3%80%81%E5%AE%9E%E7%8E%B0apply-%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.38.</span> <span class="toc-text">38、实现apply()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#39%E3%80%81%E9%98%B2%E6%8A%96"><span class="toc-number">1.0.39.</span> <span class="toc-text">39、防抖</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#40%E3%80%81%E8%8A%82%E6%B5%81"><span class="toc-number">1.0.40.</span> <span class="toc-text">40、节流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#41%E3%80%81requestAnimFrame%E5%85%BC%E5%AE%B9%E6%80%A7%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.41.</span> <span class="toc-text">41、requestAnimFrame兼容性方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#42%E3%80%81cancelAnimFrame%E5%85%BC%E5%AE%B9%E6%80%A7%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.42.</span> <span class="toc-text">42、cancelAnimFrame兼容性方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#43%E3%80%81jsonp%E5%BA%95%E5%B1%82%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.43.</span> <span class="toc-text">43、jsonp底层方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#44%E3%80%81%E8%8E%B7%E5%8F%96url%E4%B8%8A%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">1.0.44.</span> <span class="toc-text">44、获取url上的参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#45%E3%80%81%E6%A0%BC%E5%BC%8F%E5%8C%96%E6%97%B6%E9%97%B4"><span class="toc-number">1.0.45.</span> <span class="toc-text">45、格式化时间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#46%E3%80%81%E9%AA%8C%E8%AF%81%E9%82%AE%E7%AE%B1%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.0.46.</span> <span class="toc-text">46、验证邮箱的正则表达式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#47%E3%80%81%E5%87%BD%E6%95%B0%E6%9F%AF%E9%87%8C%E5%8C%96"><span class="toc-number">1.0.47.</span> <span class="toc-text">47、函数柯里化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#48%E3%80%81%E5%A4%A7%E6%95%B0%E7%9B%B8%E5%8A%A0"><span class="toc-number">1.0.48.</span> <span class="toc-text">48、大数相加</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#49%E3%80%81%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"><span class="toc-number">1.0.49.</span> <span class="toc-text">49、单例模式</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#1%E3%80%81%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D"><span class="toc-number">2.</span> <span class="toc-text">1、数组去重</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81from-%E5%8F%A0%E5%8A%A0new-Set-%E6%96%B9%E6%B3%95"><span class="toc-number">2.1.</span> <span class="toc-text">1、from()叠加new Set()方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81spread%E6%93%8D%E4%BD%9C%E7%AC%A6-%E2%80%A6"><span class="toc-number">2.2.</span> <span class="toc-text">2、spread操作符(…)</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2%E3%80%81%E6%9B%BF%E6%8D%A2%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E7%89%B9%E5%AE%9A%E5%80%BC"><span class="toc-number">3.</span> <span class="toc-text">2、替换数组中的特定值</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3%E3%80%81%E6%B2%A1%E6%9C%89map-%E7%9A%84%E6%98%A0%E5%B0%84%E6%95%B0%E7%BB%84"><span class="toc-number">4.</span> <span class="toc-text">3、没有map()的映射数组</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4%E3%80%81%E7%A9%BA%E6%95%B0%E7%BB%84"><span class="toc-number">5.</span> <span class="toc-text">4、空数组</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5%E3%80%81%E5%B0%86%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%AF%B9%E8%B1%A1"><span class="toc-number">6.</span> <span class="toc-text">5、将数组转换为对象</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6%E3%80%81%E7%94%A8%E6%95%B0%E6%8D%AE%E5%A1%AB%E5%85%85%E6%95%B0%E7%BB%84"><span class="toc-number">7.</span> <span class="toc-text">6、用数据填充数组</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7%E3%80%81%E5%90%88%E5%B9%B6%E6%95%B0%E7%BB%84"><span class="toc-number">8.</span> <span class="toc-text">7、合并数组</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8%E3%80%81%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9A%84%E4%BA%A4%E9%9B%86"><span class="toc-number">9.</span> <span class="toc-text">8、两个数组的交集</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9%E3%80%81%E5%88%A0%E9%99%A4%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%81%87%E5%80%BC"><span class="toc-number">10.</span> <span class="toc-text">9、删除数组中的假值</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10%E3%80%81%E8%8E%B7%E5%8F%96%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%9A%8F%E6%9C%BA%E5%80%BC"><span class="toc-number">11.</span> <span class="toc-text">10、获取数组中的随机值</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#11%E3%80%81lastIndexOf-%E6%96%B9%E6%B3%95"><span class="toc-number">12.</span> <span class="toc-text">11、lastIndexOf()方法</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#12%E3%80%81%E5%B0%86%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E5%80%BC%E7%9B%B8%E5%8A%A0"><span class="toc-number">13.</span> <span class="toc-text">12、将数组中的所有值相加</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/10/12/04-%E5%89%8D%E7%AB%AF%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87%EF%BC%89/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87/" title="性能优化面试题"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="性能优化面试题"/></a><div class="content"><a class="title" href="/2022/10/12/04-%E5%89%8D%E7%AB%AF%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87%EF%BC%89/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AF%87/" title="性能优化面试题">性能优化面试题</a><time datetime="2022-10-12T12:01:36.000Z" title="Created 2022-10-12 20:01:36">2022-10-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS字符串方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS字符串方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS字符串方法">JS字符串方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS对象方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS对象方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E8%AE%B0%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS对象方法">JS对象方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E6%9F%A5%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS数组方法"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS数组方法"/></a><div class="content"><a class="title" href="/2022/10/11/05-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%96%B9%E6%B3%95%E5%AE%B6%E8%A7%84%EF%BC%89/JS%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E2%80%94%E2%80%94%E9%80%9F%E6%9F%A5%E5%B0%8F%E6%9C%AC%E5%AD%90/" title="JS数组方法">JS数组方法</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%BC%80%E5%8F%91%E5%B0%81%E8%A3%85%E5%87%BD%E6%95%B0/" title="开发封装函数"><img src= "" data-lazy-src="/img/%E5%A4%A9%E7%A9%BA.jpeg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="开发封装函数"/></a><div class="content"><a class="title" href="/2022/10/11/07-%E6%A0%B8%E5%BF%83js%EF%BC%88%E6%89%8B%E5%86%99%E7%AF%87%EF%BC%89/%E5%BC%80%E5%8F%91%E5%B0%81%E8%A3%85%E5%87%BD%E6%95%B0/" title="开发封装函数">开发封装函数</a><time datetime="2022-10-11T12:01:36.000Z" title="Created 2022-10-11 20:01:36">2022-10-11</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 孙志豪</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>